package com.gongpingjialoan.widget;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.widget.AppCompatEditText;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.PopupWindow;

import com.gongpingjialoan.R;
import com.gongpingjialoan.activity.car.BuyCarVipActivity;

/**
 * Created by Administrator on 2016/3/21.
 */
public class VipBuycarPricePopupWindow extends PopupWindow implements View.OnClickListener {

    private View view;

    private View emptyView;

    private AppCompatEditText startAppCompatEditText, endAppCompatEditText;

    private Button mButton;

    private BuyCarVipActivity mBuyCarVipActivity;

    public VipBuycarPricePopupWindow(Activity context , String price) {
        super(context);
        this.mBuyCarVipActivity = (BuyCarVipActivity) context;
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.price_layout, null);
        emptyView = view.findViewById(R.id.view);
        startAppCompatEditText = (AppCompatEditText) view.findViewById(R.id.start_price);
        endAppCompatEditText = (AppCompatEditText) view.findViewById(R.id.end_price);

        if(!TextUtils.isEmpty(price)){
            try {
                String [] prices = price.split("-");
                startAppCompatEditText.setText(prices[0]);
                endAppCompatEditText.setText(prices[1]);
            } catch (Exception e) {
            }
        }
        mButton = (Button) view.findViewById(R.id.button);
        mButton.setOnClickListener(this);
        emptyView.setOnClickListener(this);
        //设置SelectPicPopupWindow的View
        this.setContentView(view);

        this.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        //设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.PopupMenu);
//        listView.setOnItemClickListener(this);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable cd = new ColorDrawable(0x000000);
        this.setBackgroundDrawable(cd);
    }


    @Override
    public void onClick(View v) {
        if (v == emptyView) {
            dismiss();
        }
        if (v == mButton) {
//            String start = startAppCompatEditText.getText().toString();
//            String end = endAppCompatEditText.getText().toString();
//
//            if (!TextUtils.isEmpty(start) && !TextUtils.isEmpty(end)) {
//                int start_p = Integer.parseInt(start);
//                int end_p = Integer.parseInt(end);
//                if (end_p < start_p) {
//                    Toast.makeText(mBuyCarVipActivity, "输入价格有误,请重新输入", Toast.LENGTH_SHORT).show();
//                } else {
////                    if (start_p >= 0 && end_p <= 5){
////                        mBuyCarVipActivity.mNoIntentFragment.otherPrice(start + "-" + end, "<5");
////                    }else if(start_p >= 30 && end_p >= 30){
////                        mBuyCarVipActivity.mNoIntentFragment.otherPrice(start + "-" + end, ">30");
////                    }else{
////                        mBuyCarVipActivity.mNoIntentFragment.otherPrice(start + "-" + end , "");
////                    }
//
//                    dismiss();
//                }
//            } else {
//                Toast.makeText(mBuyCarVipActivity, "请输入价格", Toast.LENGTH_SHORT).show();
//            }
        }
    }


}
